<?php
declare (strict_types=1);

namespace app\common\model;

use ffhome\frame\model\BaseModel;
use ffhome\frame\util\CacheUtil;
use think\facade\Db;

class TourTemplate extends BaseModel
{
    const MODEL_NAME = 'tour_template';

    /**
     * 状态:上架
     */
    const S_ENABLE = 1;

    public static function getMap(): array
    {
        return CacheUtil::get("template_map", function () {
            $list = self::getListByKey();
            if (empty($list)) {
                return [];
            }
            $provinceId = 0;
            $provinceName = '';
            $ls = [];
            $ret = [];
            foreach ($list as $vo) {
                if ($provinceId == 0) {
                    $provinceId = $vo['province_id'];
                    $provinceName = $vo['province_name'];
                }
                if ($provinceId != $vo['province_id']) {
                    $ret[] = [
                        'title' => ['id' => $provinceId, 'name' => $provinceName],
                        'list' => $ls,
                    ];
                    $provinceId = $vo['province_id'];
                    $provinceName = $vo['province_name'];
                    $ls = [];
                }
                unset($vo['province_id']);
                unset($vo['province_name']);
                $ls[] = $vo;
            }
            $ret[] = [
                'title' => ['id' => $provinceId, 'name' => $provinceName],
                'list' => $ls,
            ];
            return $ret;
        }, self::MODEL_NAME, 300);
    }

    public static function getListByKey(string $key = ''): array
    {
        return CacheUtil::get("template_list_{$key}", function () use ($key) {
            $today = date('Y-m-d');
            $domain = sysValue('domain_name');
            $where = [['r.start', '>', $today], ['t.status', '=', self::S_ENABLE], ['r.status', '=', TourRoute::S_PUBLISHING]];
            if (!empty($key)) {
                $where[] = ['t.code|t.name|province.name', 'like', "%{$key}%"];
            }
            $list = Db::name(TourRoute::MODEL_NAME)->alias('r')
                ->leftJoin(self::MODEL_NAME . ' t', "r.template_id=t.id")
                ->leftJoin(BaseArea::MODEL_NAME . ' destination', "t.destination_id=destination.id")
                ->leftJoin(BaseArea::MODEL_NAME . ' province', "destination.parent_id=province.id")
                ->field("t.id,t.code,t.name,province.id province_id,province.name province_name"
                    . ",concat('{$domain}',t.pic) pic," . self::getRouteMarketPrice())
                ->where($where)->group('t.id')->order('province.sort', 'asc')
                ->select()->toArray();
            foreach ($list as &$vo) {
                $vo['market_price'] = floatval($vo['market_price']);
            }
            return $list;
        }, self::MODEL_NAME, 600);
    }

    public static function getRouteFields(): string
    {
        $today = date('Y-m-d');
        return 'ifnull((select _r.adult_market_price from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '" order by _r.start limit 1),0) market_price,'
            . 'ifnull((select _r.adult_peer_price from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '" order by _r.start limit 1),0) peer_price,'
            . 'ifnull((select _r.adult_cost_price from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '" order by _r.start limit 1),0) cost_price,'
            . '(select count(_r.id) from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '") route_count';
    }

    public static function getRouteMarketPrice(): string
    {
        $today = date('Y-m-d');
        return 'ifnull((select _r.adult_market_price from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '" order by _r.start limit 1),0) market_price,'
            . '(select count(_r.id) from e_tour_route _r where _r.template_id=t.id and _r.start>"'
            . $today . '") route_count';
    }

    public static function getById($id): array
    {
        if (empty($id)) return [];
        return Db::name(self::MODEL_NAME)->field('id,code,name')->where('id', $id)->find();
    }
}